/* Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.mdp.workflow.ru.ctrl;

import com.mdp.workflow.de.service.ModelServiceImpl;
import org.apache.commons.io.IOUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.flowable.bpmn.model.BpmnModel;
import org.flowable.engine.HistoryService;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.image.ProcessDiagramGenerator;
import org.flowable.spring.SpringProcessEngineConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import java.util.List;

@RestController
public class DiagramController {

	@Autowired
	private RuntimeService runtimeService;
  
  	@Autowired
	private RepositoryService repositoryService;
  
  	@Autowired
	private HistoryService historyService;
  
	@Autowired
	private ModelServiceImpl modelService;
  
	@Autowired	
	SpringProcessEngineConfiguration engineConfig;

	@Value(value = "${mdp.platform-branch-id:platform-branch-001}")
	String platformBranchId;
	
	Log log=LogFactory.getLog(DiagramController.class);
	
	/**
	* 获得流程图的流
	* @param processInstanceId
	* @return
	*/
	@RequestMapping(value="/**/workflow/ru/diagram/{processDefinitionId}/{processInstanceId}", method = RequestMethod.GET)
	public void getProcessInstanceDiagram(@PathVariable String processInstanceId,@PathVariable String processDefinitionId,HttpServletResponse response){
	//查询流程实例
		ProcessInstance pi = this.runtimeService.createProcessInstanceQuery().processInstanceId(processInstanceId).singleResult();
		if(pi==null){
			this.getProcessDefinitionDiagram(processDefinitionId, response);
			return;
		}
		BpmnModel bpmnModel = this.repositoryService.getBpmnModel(pi.getProcessDefinitionId());
		// 得到正在执行的环节
		List<String> activeIds = this.runtimeService.getActiveActivityIds(pi.getId());
		log.debug("字体----》getLabelFontName："+engineConfig.getLabelFontName()+" getActivityFontName:"+engineConfig.getActivityFontName());
		//List<String> highlightedFlows=this.getHighLightedFlows((ProcessDefinitionEntity)repositoryService.createProcessDefinitionQuery().processDefinitionId(pi.getProcessDefinitionId()).singleResult(), processInstanceId);
		ProcessDiagramGenerator diagramGenerator = engineConfig.getProcessDiagramGenerator();
        InputStream is = diagramGenerator.generateDiagram(bpmnModel, "jpg",activeIds, Collections.<String>emptyList(), engineConfig.getActivityFontName(),
        		engineConfig.getLabelFontName(), engineConfig.getAnnotationFontName(), engineConfig.getClassLoader(), 1.0, true);
		 
		try {
			IOUtils.copy(is, response.getOutputStream());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	/**
	* 获得流程图的流
	* @param processDefinitionId
	* @return
	*/
	@RequestMapping(value="/**/workflow/ru/diagram/{processDefinitionId}", method = RequestMethod.GET)
	public void getProcessDefinitionDiagram(@PathVariable String processDefinitionId,HttpServletResponse response){
	
		BpmnModel bpmnModel = this.repositoryService.getBpmnModel(processDefinitionId);
		ProcessDiagramGenerator diagramGenerator = engineConfig.getProcessDiagramGenerator();
		log.debug("字体----》getLabelFontName："+engineConfig.getLabelFontName()+" getActivityFontName:"+engineConfig.getActivityFontName());
		
        InputStream is = diagramGenerator.generateDiagram(bpmnModel, "bmp", Collections.<String>emptyList(), Collections.<String>emptyList(), engineConfig.getActivityFontName(),
        		engineConfig.getLabelFontName(), engineConfig.getAnnotationFontName(), engineConfig.getClassLoader(), 1.0, true);
		 
		try {
			IOUtils.copy(is, response.getOutputStream());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
}
